using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class HistoryRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class HistoryDbo : DatabaseObject
        {
            public HistoryDbo()
            {
                Name = "History";
                GeneratedClassName = "History";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "int", IsIdentity = true, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CountryId", ColumnType = "smallint", IsIdentity = false, IsNullable = false, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "CountryId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Title", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Title" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "RangeReviewId", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "RangeReviewId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Rate", ColumnType = "decimal", IsIdentity = false, IsNullable = false, Length = 9, Precision = 18, Scale = 8, GeneratedPropertyName = "Rate" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SpacesConversion", ColumnType = "decimal", IsIdentity = false, IsNullable = false, Length = 9, Precision = 18, Scale = 8, GeneratedPropertyName = "SpacesConversion" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "FuelsConversion", ColumnType = "decimal", IsIdentity = false, IsNullable = false, Length = 9, Precision = 18, Scale = 8, GeneratedPropertyName = "FuelsConversion" });
              
            }
        }
		
		public static List<HistoryRow> Select(Expression<Func<HistoryRow, bool>> expression)
        {
            List<HistoryRow> rows = new List<HistoryRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<HistoryRow>(new HistoryDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<HistoryRow> Select()
		{
			List<HistoryRow> rows = new List<HistoryRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<HistoryRow>(new HistoryDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<HistoryRow> PagedSelect(Expression<Func<HistoryRow, bool>> filterExpression, Expression<Func<HistoryRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<HistoryRow> rows = new List<HistoryRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<HistoryRow>(new HistoryDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<HistoryRow> HistoryList)
        {
            HistoryRow row;

            while (reader.Read())
            {
                row = new HistoryRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					CountryId = Util.CastDbValue<int>(reader["CountryId"]),
					Title = Util.CastDbValue<string>(reader["Title"]),
					RangeReviewId = Util.CastDbValue<int?>(reader["RangeReviewId"]),
					Rate = Util.CastDbValue<decimal>(reader["Rate"]),
					SpacesConversion = Util.CastDbValue<decimal>(reader["SpacesConversion"]),
					FuelsConversion = Util.CastDbValue<decimal>(reader["FuelsConversion"])
                };
                                
                HistoryList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(HistoryRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<HistoryRow>(new HistoryDbo(), row);
            }
        }

		public static void Update(HistoryRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<HistoryRow>(new HistoryDbo(), row);
            }
        }

		public static void Delete(Expression<Func<HistoryRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<HistoryRow>(new HistoryDbo(), expression);
            }
        }

	}
}
	
